/*******************************************************************************
 * Dreampalace
 *******************************************************************************
 * 
 * Dreampalace is a special dreamdiary which helps dreamers to understand their
 * dreams. You can find also many features in Dreampalace to learn and hold the
 * lucid state in your dreams.
 * 
 * *****************************************************************************
 *
 * Program	   : Dreampalace
 * Autor           : PRo (Peter Rogge)
 * Email           : rogge-peter@gmx.de
 * Homepage        : http://code.google.com/p/dreampalace
 * License	   : GNU General Public License 3.0
 * 
 *******************************************************************************
 * 
 * This file is part of the project Dreampalace. Dreampalace is free software:
 * you can redistribute it and/or modify it under the terms of the GNU General 
 * Public License as published by the Free Software Foundation in version 3 of
 * the License only.
 * 
 * Dreampalace is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Dreampalace. If not, see <http://www.gnu.org/licenses/>.
 * 
 * The Dreampalace project designates this particular file as subject to the
 * "Classpath" exception as provided by the Dreampalace project in the GPL 
 * Version 3 section of the License file that accompanied this code.
 * 
 *******************************************************************************
 */
package de.pro.dreampalace.util;

import de.pro.dreampalace.util.api.IUtilManager;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.MissingResourceException;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

/**
 *
 * @author Peter Rogge (PRo) <rogge-peter@gmx.de>
 */
@ServiceProvider(service=IUtilManager.class)
public class UtilManager implements IUtilManager {

    @Override
    public String getMessage(String key) {
        try {
            return NbBundle.getMessage(this.getClass(), key);
        } catch(MissingResourceException  mre) { }
        
        return "-- key(" + key + ") not found --";
    }
    
    /**
     * Read the whole text in the present <code>Clob</clob>.
     *
     * @param clob
     * @return
     * @throws SQLException
     */
    @Override
    public final String getTextFromClob(final Clob clob) throws SQLException {
        final long length = clob.length();
        final boolean isInRange = (length <= Integer.MAX_VALUE);
        if (isInRange) {
            return clob.getSubString(1, Integer.parseInt(String.valueOf(length)));
        }

        final StringBuffer text = new StringBuffer();
        int rounds = 0;
        for (long pos = 1; pos < length; pos += Integer.MAX_VALUE) {
            text.append(clob.getSubString(pos, Integer.MAX_VALUE));
            ++rounds;
        }

        int rest = Integer.parseInt(String.valueOf(length - Integer.MAX_VALUE * rounds));
        if (rest > 0) {
            text.append(clob.getSubString(length - rest, rest));
        }

        return text.toString();
    }
}
